package 自己刷题;

/*
 * @author  YxinMiracle
 * @date  2022-05-03 10:15
 * @Gitee: https://gitee.com/yxinmiracle
 */


import java.util.List;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}


public class 两数相加 {
    public ListNode result = new ListNode(0);
    public ListNode temp = result;
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();
        while (l1 != null){
            int val = l1.val;
            l1 = l1.next;
            s1.insert(0, val);
        }
        while (l2 != null){
            int val = l2.val;
            l2 = l2.next;
            s2.insert(0, val);
        }
        int sum = Integer.parseInt(s1.toString()) + Integer.parseInt(s2.toString());
        String stringSum = String.valueOf(sum);
        for (int i = stringSum.length()-1; i >= 0 ; i--) {
            temp.next = new ListNode(Integer.parseInt(stringSum.charAt(i)+""));
            temp = temp.next;
        }
        return result.next;
    }

}
